package com.sl.javatest;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class MyClass {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
        // 反射获取类对象
        Class<Person> a = Person.class;
        Class<?> b = a.getClass();
        Class<?> c = Class.forName("com.sl.javatest.Person");
        Class<?> d = c.getClassLoader().loadClass("com.sl.javatest.Person");
        Class<Person> e = (Class<Person>) Boy.class.getSuperclass();

       // 根据类获取类名
        Class<?> p = Class.forName("com.sl.javatest.Person");
//        System.out.println(p.getName());
//        System.out.println(p.getSimpleName());
//        System.out.println(p.getPackage());
//        System.out.println(String[].class.getCanonicalName());
//        System.out.println(String[].class.getName());

        //获取属性
        try{
            Field nick = p.getField("nick");
            System.out.println(nick.getName());
            int modifier = nick.getModifiers();
            String s = Modifier.toString(modifier);
            System.out.println(modifier + s );

            Class<?> type = nick.getType();
            System.out.println(type);
            System.out.println(type.getName());

//            Field[] declaredFields = p.getDeclaredFields();
            Field age = p.getDeclaredField("age");
            System.out.println(age);

            Person o = (Person) p.newInstance();
            age.setAccessible(true);
           age.set(o,14);
            int ageValue  = (int) age.get(o);

            System.out.println(ageValue);

//            Field[] f  = p.getFields();
//            for (int i = 0; i < declaredFields.length; i++) {
//                System.out.println(declaredFields[i].getName());
//            }
        }catch (Exception exception){
            exception.printStackTrace();
        }

        System.out.println("----------");
        // Method
        Method[] methods = p.getMethods();
        for (int i = 0; i < methods.length; i++) {
            System.out.println(methods[i].getName());
        }
        System.out.println("----------");

        Method hashCode = p.getMethod("hashCode");

        System.out.println("---------- " + hashCode);

        Method[] declaredMethods = p.getDeclaredMethods();
        System.out.println("----------");


        for (int i = 0; i < declaredMethods.length; i++) {
            System.out.println(declaredMethods[i].getName());
        }

        System.out.println("----------");

        Method initX = p.getDeclaredMethod("initX");
        initX.setAccessible(true);
        System.out.println(initX);
        Person m = new Person();
        try {
            initX.invoke(m);
        } catch (IllegalAccessException illegalAccessException) {
            illegalAccessException.printStackTrace();
        } catch (InvocationTargetException invocationTargetException) {
            invocationTargetException.printStackTrace();
        }
        System.out.println("----------");
        Class<?>[] parameterTypes = initX.getParameterTypes();
        for (int i = 0; i < parameterTypes.length; i++) {
            System.out.println(parameterTypes[i].getName());
        }
        System.out.println("----------");
        Class<?> returnType = initX.getReturnType();
        System.out.println(returnType.getName());
        System.out.println("----------");

        Constructor<Person> constructor = Person.class.getConstructor(String.class);
        System.out.println("0 -- : "  +  constructor);

        Constructor<?>[] constructors = Person.class.getConstructors();
        Constructor<?>[] declaredConstructors = Person.class.getDeclaredConstructors();

        for (int i = 0; i < 5; i++) {
            if (constructors.length > i){
                System.out.println("1---" + constructors[i]);
            }
            if (declaredConstructors.length > i){
                System.out.println("2---" + declaredConstructors[i]);
            }
        }

    }
}